package cs.sy.service.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class UserFilter implements Filter{

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		String path = request.getServletPath();
		if(request.getSession().getAttribute("user") == null && !path.equals("/perfectfood") 
				&& !path.equals("/u_login.jsp") && !path.equals("/userlogin") 
				&& !path.contains("/js") && !path.contains("/css") && !path.equals("/index.jsp")
				&& !path.contains("/img") && !path.contains("/register")){
			req.getRequestDispatcher("/u_login.jsp").forward(req, resp);
		}else{
			chain.doFilter(req, resp);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
